package christ.mmd.xml;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import christ.mmd.bl.Community;
import christ.mmd.bl.FileUpdateInfo;
import christ.mmd.bl.reading.Reading;
import christ.mmd.bl.reading.Verse;
import christ.mmd.bl.reading.Zachalo;

public class ReadingsXmlParser extends AXmlParser {

	@Override
	protected List readDataEntities(XmlPullParser parser)
			throws XmlPullParserException, IOException {
		List entries = new ArrayList();

		parser.require(XmlPullParser.START_TAG, ns, "readings");
		while (parser.next() != XmlPullParser.END_TAG) {
			if (parser.getEventType() != XmlPullParser.START_TAG) {
				continue;
			}
			String name = parser.getName();
			// Starts by looking for the entry tag
			if (name.equals("reading")) {
				entries.add(readReadingEntry(parser));
			} else {
				skip(parser);
			}
		}
		return entries;
	}

	// Processes file tags in the feed.
	private Reading readReadingEntry(XmlPullParser parser)
			throws IOException, XmlPullParserException {
		parser.require(XmlPullParser.START_TAG, ns, "reading");

		String id = parser.getAttributeValue(null, "id");
		String dateStr = parser.getAttributeValue(null, "date");
		String officeType = parser.getAttributeValue(null, "officeType");
		ArrayList<Zachalo> zachaloList = new ArrayList<Zachalo>();
		parser.nextTag();
		String tagName = parser.getName();
		// Starts by looking for the entry tag
		while (tagName.equals("zachalo")) {
			zachaloList.add(readZachalo(parser));
			parser.nextTag();
			tagName = parser.getName();
		}
		parser.require(XmlPullParser.END_TAG, ns, "reading");
		SimpleDateFormat format = new SimpleDateFormat("dd/MM/yy");
		Date date = null;
		try {
			date = format.parse(dateStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Reading result = new Reading(Long.parseLong(id), date, officeType,  zachaloList);
		return result;
	}

	private Zachalo readZachalo(XmlPullParser parser) throws XmlPullParserException, IOException {
		parser.require(XmlPullParser.START_TAG, ns, "zachalo");
		String type = parser.getAttributeValue(null, "type");
		String book = parser.getAttributeValue(null, "book");
		String number = parser.getAttributeValue(null, "number");
		ArrayList<Verse> verseList = new ArrayList<Verse>();
		parser.nextTag();
		String tagName = parser.getName();
		// Starts by looking for the entry tag
		while (tagName.equals("verse")) {
			verseList.add(readVerse(parser));
			parser.nextTag();
			tagName = parser.getName();
		}
		parser.require(XmlPullParser.END_TAG, ns, "zachalo");
		return new Zachalo(type, book, Integer.parseInt(number), verseList);
	}

	private Verse readVerse(XmlPullParser parser) throws XmlPullParserException, IOException {
		parser.require(XmlPullParser.START_TAG, ns, "verse");
		String paragraphFrom = parser.getAttributeValue(null, "pFrom");
		String verseFrom = parser.getAttributeValue(null, "vFrom");
		String paragraphTo = parser.getAttributeValue(null, "pTo");
		String verseTo = parser.getAttributeValue(null, "vTo");
		parser.nextTag();
		parser.require(XmlPullParser.END_TAG, ns, "verse");		
		return new Verse(Integer.parseInt(paragraphFrom), Integer.parseInt(verseFrom), Integer.parseInt(paragraphTo), Integer.parseInt(verseTo));
	}
}
